package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.QueryObject;
import cn.wolfcode.wolf2w.search.domain.DestinationEs;
import cn.wolfcode.wolf2w.search.domain.StrategyEs;
import cn.wolfcode.wolf2w.search.domain.TravelEs;
import cn.wolfcode.wolf2w.search.domain.UserInfoEs;
import cn.wolfcode.wolf2w.search.query.SearchQueryObject;
import cn.wolfcode.wolf2w.search.service.IDestinationESService;
import cn.wolfcode.wolf2w.search.service.ISearchService;
import cn.wolfcode.wolf2w.search.service.IStrategyESService;
import cn.wolfcode.wolf2w.search.vo.SearchResultVO;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.ParamMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SearchController {

    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ISearchService searchService;


    @GetMapping("/q")
    public Object search(SearchQueryObject qo) throws UnsupportedEncodingException {
        //处理url解码
        String decode = URLDecoder.decode(qo.getKeyword(), "utf-8");
        qo.setKeyword(decode);
        //通过type的类型，进行查找相应的类型
            if(qo.getType()==0){
                return this.searchDest(qo);
            }else if(qo.getType()==1){
                return this.searchStrategy(qo);
            }else if(qo.getType()==2){
                return this.searchTravel(qo);
            }else if(qo.getType()==3){
                return this.searchUserInfo(qo);
            }else{
                return this.searchAll(qo);
            }
    }

    private Object searchAll(SearchQueryObject qo) {
        SearchResultVO vo=new SearchResultVO();
        Long total=0L;//统计数据总条数
        Page<Destination> ds = this.createDestination(qo);
        vo.setDests(ds.getContent());
        total+=ds.getTotalElements();
        Page<Strategy> ss=this.createStrategy(qo);
        vo.setStrategys(ss.getContent());
        total+=ss.getTotalElements();
        Page<Travel> ts=this.createTravel(qo);
        vo.setTravels(ts.getContent());
        total+=ts.getTotalElements();
        Page<UserInfo> us=this.createUserInfo(qo);
        vo.setUsers(us.getContent());
        total+=us.getTotalElements();

        //Map<String,Object> map=new HashMap<>();
        //map.put("result", vo);
        //map.put("qo", qo);

        ParamMap map = ParamMap.instance().put("result", vo).put("qo", qo);
        return JsonResult.success(map);
    }

    private Page<UserInfo> createUserInfo(SearchQueryObject qo) {
        Page<UserInfo> page = this.getPage(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo, "info", "city");
        return page;
    }

    private Page<Travel> createTravel(SearchQueryObject qo) {
        Page<Travel> page = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class, qo, "title","summary");
        return page;
    }

    private Page<Strategy> createStrategy(SearchQueryObject qo) {
        Page<Strategy> page = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo, "title", "subTitle", "summary");
        return page;
    }

    private Page<Destination> createDestination(SearchQueryObject qo) {
        Page<Destination> page = searchService.searchWithHighlight(DestinationEs.INDEX_NAME, DestinationEs.TYPE_NAME, Destination.class,qo,"info","name");
        return page;
    }

    private Object searchUserInfo(SearchQueryObject qo) {
        Page<UserInfo> page = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class,qo,"info","city");
        //Map<String,Object> map=new HashMap<>();
        //map.put("page", page);
        //map.put("qo", qo);
        ParamMap map = ParamMap.instance().put("page", page).put("qo", qo);
        return JsonResult.success(map);
    }

    private Object searchTravel(SearchQueryObject qo) {
        Page<Travel> page = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class, qo, "title","summary");
        //Map<String,Object> map=new HashMap<>();
        //map.put("page", page);
        //map.put("qo", qo);
        ParamMap map = ParamMap.instance().put("page", page).put("qo", qo);
        return JsonResult.success(map);
    }

    private Object searchStrategy(SearchQueryObject qo) {
        Page<Strategy> page = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo, "title", "subTitle", "summary");
        //Map<String,Object> map=new HashMap<>();
        //map.put("page", page);
        //map.put("qo", qo);
        ParamMap map = ParamMap.instance().put("page", page).put("qo", qo);
        return JsonResult.success(map);
    }
    /**
     * 目的地精确查询
     * 需要返回result、dest、qo
     * 其中result：total、strategys、travels、users
     * 将三个数值以map集合的方式返回给前端
     * @param qo
     * @return
     */
    private Object searchDest(SearchQueryObject qo) {
        Map<String,Object> map=new HashMap<>();
            //通过关键字查询出目的地
            Destination destination=destinationService.queryByKeyword(qo.getKeyword());
            if(destination!=null){
                //通过目的地的名称查出所有的攻略
                List<Strategy> strategys= strategyService.queryByDestName(destination.getName());
                //通过目的地的名称查出所有的游记
                List<Travel> travels=travelService.queryByDestName(destination.getName());
                //用户通过所在城市进行查找
                List<UserInfo> userInfos=userInfoService.queryByCity(destination.getName());

                SearchResultVO vo=new SearchResultVO();
                vo.setTotal(strategys.size()+travels.size()+userInfos.size()+0L);
                vo.setStrategys(strategys);
                vo.setTravels(travels);
                vo.setUsers(userInfos);
                map.put("result", vo);
                map.put("dest", destination);
                map.put("qo", qo);
            }

            return JsonResult.success(map);
    }



    public <T> Page<T> getPage(String index,String type, Class clz, SearchQueryObject qo,String... fields){
        Page page = searchService.searchWithHighlight(index, type, clz, qo, fields);
        return page;
    }

}
